/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-07-16     82752       the first version
 */
#include "lora_json_msg_obj.h"

char *create_json_th_sensor_msg(struct th_msg *msg)
{
    char *string = NULL;
    cJSON *wsn_type = NULL;

    cJSON *sensor_type = NULL;
    cJSON *addr = NULL;
    cJSON *items = NULL;
    cJSON *temperature_value = NULL;
    cJSON *humid_value = NULL;

    cJSON *json_ogj = cJSON_CreateObject();
    if (json_ogj == NULL)
    {
        goto end;
    }

    wsn_type = cJSON_CreateNumber(msg->base_property.wsn_type);
    if (wsn_type == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "wsn_type", wsn_type);

    sensor_type = cJSON_CreateNumber(msg->base_property.sensor_type);
    if (sensor_type == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "sensor_type", sensor_type);

    addr = cJSON_CreateNumber(msg->base_property.addr);
    if (addr == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "addr", addr);

    items = cJSON_CreateObject();
    if (items == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "items", items);

    cJSON *temperature_obj = cJSON_CreateObject();
    if (temperature_obj == NULL)
    {
        goto end;
    }
    temperature_value = cJSON_CreateNumber(msg->temp_value);
    if (temperature_value == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(temperature_obj, "value", temperature_value);
    cJSON_AddItemToObject(items, "temprature", temperature_obj);


    cJSON *humid_obj = cJSON_CreateObject();
    if (humid_obj == NULL)
    {
        goto end;
    }
    humid_value = cJSON_CreateNumber(msg->humid_value);
    if (humid_value == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(humid_obj, "value", humid_value);
    cJSON_AddItemToObject(items, "humid", humid_obj);

    string = cJSON_Print(json_ogj);
    if (string == NULL)
    {
        LOG_E("Failed to print sensor msg.\n");
    }

end:
    cJSON_Delete(json_ogj);
    return string;
}


char *create_json_light_sensor_msg(struct light_msg *msg)
{
    char *string = NULL;
    cJSON *wsn_type = NULL;

    cJSON *sensor_type = NULL;
    cJSON *addr = NULL;
    cJSON *items = NULL;
    cJSON *light_value = NULL;

    cJSON *json_ogj = cJSON_CreateObject();
    if (json_ogj == NULL)
    {
        goto end;
    }

    wsn_type = cJSON_CreateNumber(msg->base_property.wsn_type);
    if (wsn_type == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "wsn_type", wsn_type);

    sensor_type = cJSON_CreateNumber(msg->base_property.sensor_type);
    if (sensor_type == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "sensor_type", sensor_type);

    addr = cJSON_CreateNumber(msg->base_property.addr);
    if (addr == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "addr", addr);

    items = cJSON_CreateObject();
    if (items == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(json_ogj, "items", items);

    cJSON *light_obj = cJSON_CreateObject();
    if (light_obj == NULL)
    {
        goto end;
    }
    light_value = cJSON_CreateNumber(msg->light_value);
    if (light_value == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(light_obj, "value", light_value);
    cJSON_AddItemToObject(items, "light", light_obj);

    string = cJSON_Print(json_ogj);
    if (string == NULL)
    {
        LOG_E("Failed to print sensor msg.\n");
    }

end:
    cJSON_Delete(json_ogj);
    return string;
}

